package org.red_cross_service.utils;

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Map;

public class HttpUtils {

    /**
     * 发送 GET 请求
     *
     * @param url 请求的 URL
     * @return 响应的内容
     * @throws IOException 如果发生网络错误
     */
    public static JSONObject sendGet(String url) {
        try {
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
            con.setConnectTimeout(5000);
            con.setReadTimeout(5000);
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            return JSONObject.parseObject(response.toString());
        } catch (IOException e) {
            return JSONObject.parseObject(e.getMessage());
        }
    }

    /**
     * 发送 POST 请求
     *
     * @param url    请求的 URL
     * @param params 请求的参数，以键值对的形式存储
     * @return 响应的内容
     * @throws IOException 如果发生网络错误
     */
    public static JSONObject sendPost(String url, Map<String, String> params) {
        try {
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("POST");
            con.setConnectTimeout(5000);
            con.setReadTimeout(5000);
            con.setDoOutput(true);
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String, String> param : params.entrySet()) {
                if (postData.length() != 0) {
                    postData.append('&');
                }
                postData.append(param.getKey());
                postData.append('=');
                postData.append(param.getValue());
            }
            byte[] postDataBytes = postData.toString().getBytes(StandardCharsets.UTF_8);

            // 设置请求头
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            con.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));

            // 发送 POST 参数
            try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
                wr.write(postDataBytes);
            }
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            return JSONObject.parseObject(response.toString());
        } catch (IOException e) {
            return JSONObject.parseObject(e.getMessage());
        }

    }
}